package com.ruoyi.plot.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.plot.domain.dto.request.PlotQueryForPageReqDTo;
import com.ruoyi.plot.domain.dto.request.PlotSaveOrUpdateReqDTo;
import com.ruoyi.plot.domain.dto.response.PlotInfoResDTO;
import com.ruoyi.plot.service.IPlotService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 小区功能列表Controller
 * 
 * @author ruoyi
 * @date 2024-09-13
 */
@RestController
@RequestMapping("/plot/plot")
public class PlotController extends BaseController
{
    @Autowired
    private IPlotService plotService;

    @Autowired
    private ElasticsearchRestTemplate elasticsearchRestTemplate;

    /**
     * 查询小区功能列表列表
     */
    @PreAuthorize("@ss.hasPermi('plot:plot:list')")
    @GetMapping("/list")
    public TableDataInfo list(
        PlotQueryForPageReqDTo plotQueryForPageReqDTo
    )
    {
        startPage();
        List<PlotInfoResDTO> list = plotService.selectPlotList(plotQueryForPageReqDTo);
        return getDataTable(list);
    }

    /**
     * 导出小区功能列表列表
     */
    @PreAuthorize("@ss.hasPermi('plot:plot:export')")
    @Log(title = "小区功能列表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PlotQueryForPageReqDTo plotQueryForPageReqDTo)
    {
        List<PlotInfoResDTO> list = plotService.selectPlotList(plotQueryForPageReqDTo);
        ExcelUtil<PlotInfoResDTO> util = new ExcelUtil<PlotInfoResDTO>(PlotInfoResDTO.class);
        util.exportExcel(response, list, "小区功能列表数据");
    }

    /**
     * 获取小区功能列表详细信息
     */
    @PreAuthorize("@ss.hasPermi('plot:plot:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(plotService.selectPlotById(id));
    }

    /**
     * 新增小区功能列表
     */
    @PreAuthorize("@ss.hasPermi('plot:plot:add')")
    @Log(title = "小区功能列表", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PlotSaveOrUpdateReqDTo plotSaveOrUpdateReqDTo)
    {
        return toAjax(plotService.insertPlot(plotSaveOrUpdateReqDTo));
    }

    /**
     * 修改小区功能列表
     */
    @PreAuthorize("@ss.hasPermi('plot:plot:edit')")
    @Log(title = "小区功能列表", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PlotSaveOrUpdateReqDTo plotSaveOrUpdateReqDTo)
    {
        return toAjax(plotService.updatePlot(plotSaveOrUpdateReqDTo));
    }

    /**
     * 批量删除小区功能列表
     */
    @PreAuthorize("@ss.hasPermi('plot:plot:remove')")
    @Log(title = "小区功能列表", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(plotService.deletePlotByIds(ids));
    }

    /**
     * 删除小区功能列表
     */
    @PreAuthorize("@ss.hasPermi('plot:plot:remove')")
    @Log(title = "小区功能列表", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public AjaxResult removeById(@PathVariable Long id)
    {
        return toAjax(plotService.deletePlotById(id));
    }
}
